<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

require_once 'database.php';

class photo_uploader extends database{
    public $photo_url;
    public $photo_id;

    function  __construct() {
        parent::__construct();
    }

    function  __destruct() {

    }

    function upload_profpic($usr_id, $file){
        
        //bikin folder dulu, kalo gak ada
        if (!is_dir("../images/photos/". $usr_id)){
            mkdir("../images/photos/". $usr_id);
        }
		$date_now = date('Y-m-d');
        $target_path = "../images/photos/". $usr_id. "/". basename($file['name']);
        $tr = str_replace("../", "",$target_path);
        $query = "INSERT INTO photo (usr_owner,url,date_upload) VALUES ('$usr_id','$tr','$date_now')";
        if (move_uploaded_file($file['tmp_name'], $target_path)){
            $this->connect();
            //masukin ke DB
            mysql_query($query,$this->conn);
            mysql_close();

            //get photo_id
            $query2 = "SELECT max(photo_id) AS maks FROM photo";
            $this->exec_q($query2, "maks");
            $id = $this->result[0];
            $this->photo_url = $tr;
            $this->photo_id = $id;
        
        }
    }

    function upload_photo($usr_id, $deskripsi, $tag, $date, $kategori, $privacy, $allow_comment, $file){

        //bikin folder dulu, kalo gak ada
        if (!is_dir("images/photos/". $usr_id)){
            mkdir("images/photos/". $usr_id);
        }
		$date_now = date('Y-m-d');
        $target_path = "images/photos/". $usr_id. "/". basename($file['name']);
        $query = "INSERT INTO photo (usr_owner,url,tag,description,date_capt,date_upload,category,privacy,com_permission) VALUES ('$usr_id','$target_path','$tag','$deskripsi','$date','$date_now','$kategori','$privacy','$allow_comment')";
        if (move_uploaded_file($file['tmp_name'], $target_path)){
            $this->connect();
            //masukin ke DB
            mysql_query($query,$this->conn);
            mysql_close();
			
			//get photo_id
            $query2 = "SELECT max(photo_id) AS maks FROM photo";
            $this->exec_q($query2, "maks");
            $id = $this->result[0];
            $this->photo_url = $tr;
            $this->photo_id = $id;
			return $id;
        }
    }

    function set_profpic_id($usr_id, $id){
        $query = "UPDATE user SET profpic_id=$id WHERE usr_email='$usr_id'";
        $this->connect();
            //masukin ke DB
        mysql_query($query,$this->conn);
        mysql_close();
    }
}

?>